﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Cosmetic.DataEntity;
using Cosmetic.Logic;
using System.Collections.Generic;

namespace WebApp.Management.Ad
{
    public partial class addxinwclass : PageBase2
    {
        private  articleclassManger articleclassmanger = new articleclassManger();

        private RoleManager m_RoleManager = new RoleManager();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.SignInUser1 == null)
            {
                Session.Remove("SignInUser1");
                this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('请先登录权限!');window.top.location.href='/Management/indexs.aspx';", true);
                return;
            }
            if (!IsPostBack)
            {
                Cosmetic.DataEntity.User theUser = this.SignInUser1;
                if (!this.m_RoleManager.CheckUserRole(theUser, RoleManager.ERole.Admin))
                {
                    Session.Remove("SignInUser1");
                    this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('你没有管理员权限!');window.top.location.href='/Management/indexs.aspx';", true);
                    return;
                }
            }

            if (!IsPostBack)
            {
                //this.LoadFitCategory();
                //新增文章类别
                if (String.IsNullOrEmpty(this.Request.QueryString["PID"]))
                {
                    this.btnEditProduct.Visible =true  ;
                    this.Button1.Visible = false;
                }
                //修改文章类别
                else
                {
                    this.btnEditProduct.Visible = false;
                    this.Button1.Visible = true;
                    this.LoadProduct(Convert.ToInt32(this.Request.QueryString["PID"]));
                }
            }

        }
        private void LoadProduct(int aProductId)
        {
            articleclass theProduct = this.articleclassmanger.GetProductById(aProductId);

            this.TextBox1.Text = theProduct.name;
        }

        protected void btnEditProduct_Click(object sender, EventArgs e)
        {
            articleclass numbers = new articleclass();
            if (string.IsNullOrEmpty(this.TextBox1 .Text .Trim ()))
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('请填写类别!');", true);
                return;
            }
            numbers.name = this.TextBox1.Text.Trim();
            this.articleclassmanger.Insert(numbers);
            this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('添加成功!');", true);
            this.TextBox1.Text = "";
            
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            articleclass theProduct = this.articleclassmanger.GetProductById(Convert.ToInt32(this.Request.QueryString["PID"]));

            if (string.IsNullOrEmpty(this.TextBox1 .Text.Trim()))
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('请填写类别!');", true);
                return;
            }
            theProduct.name  = this.TextBox1.Text;
            this.articleclassmanger.Update();
            this.ClientScript.RegisterStartupScript(this.GetType(), this.Title, "alert('Edit Succeed!');", true);
            this.Page.Response.Redirect("xinwenslist.aspx");

        }
    }
}
